//
//  LCFirstTrialViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/4/10.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCFirstTrialViewController.h"
#import "LCTaskService.h"
#import "LCTaskModel.h"

#import "XWXAllXieYiController.h"
@interface LCFirstTrialViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTfl;

@property (weak, nonatomic) IBOutlet UITextField *cardNoTfl;

@property (weak, nonatomic) IBOutlet UITextField *phoneTfl;

@property (strong, nonatomic)NSString *applyUrl;

@end

@implementation LCFirstTrialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)submitBtnAction:(UIButton *)sender {
    if (self.nameTfl.text.length <= 0) {
        [self showErrorText:self.nameTfl.placeholder];
    } else if (self.cardNoTfl.text.length <= 0) {
        [self showErrorText:self.cardNoTfl.placeholder];
    } else if (![NSString isIdCardNumber:self.cardNoTfl.text]) {
        [self showErrorText:@"请输入正确的身份证件号码"];
    } else if (self.phoneTfl.text.length <= 0) {
        [self showErrorText:self.phoneTfl.placeholder];
    } else if (![NSString isMobileNumber:self.phoneTfl.text]) {
        [self showErrorText:@"请输入正确的手机号码"];
    } else {
        NSMutableDictionary * param = [NSMutableDictionary dictionary];
        
        param[@"cid"] = kFORMAT(@"%@", self.task.platforminfo.accessid);
        NSString * timestamp = [NSString GetTimeIntervalWithDate:[NSDate date]];
        param[@"timestamp"] = timestamp;
        param[@"productId"] = kFORMAT(@"%@", self.task.platforminfo.unionid);
        param[@"subOrderId"] = kFORMAT(@"%@%@",timestamp, LCUserDefGet(Userid));
        param[@"applyName"] = self.nameTfl.text;
        param[@"applyMobile"] = self.phoneTfl.text;
        param[@"applyIdCard"] = self.cardNoTfl.text;
        
        [LCTaskService getApplyUrlWithParam:param Salt:self.task.platforminfo.accesskey Block:^(BOOL isSuccess, NSString *applyUrl) {
            if (isSuccess) {
                self.applyUrl = applyUrl;
                if ([self.task.downloadid integerValue] == 0) {
                    [self downloadTaskApp];
                } else {
                    [self openTaskApp];
                }
            }
        }];
    }
}

- (void)downloadTaskApp {
    [LCTaskService downloadTaskAppWithAppID:self.task.appid LaunchID:self.task.launchid Block:^(BOOL isSuccess, NSDictionary *field) {
        if (isSuccess) {
            [self uploadTaskWithDict:field];
        }
    }];
}

- (void)openTaskApp {
    [LCTaskService openTaskAppWithDownloadID:self.task.downloadid Block:^(BOOL isSuccess, NSDictionary *field) {
        if (isSuccess) {
            [self uploadTaskWithDict:field];
        }
    }];
}

- (void)uploadTaskWithDict:(NSDictionary *)dict {
    [LCTaskService uploadTaskWithRewardID:dict[@"rewardid"] RewardKey:dict[@"rewardkey"] Name:self.nameTfl.text IdCard:self.cardNoTfl.text Phone:self.phoneTfl.text Block:^(BOOL isSuccess) {
        if (isSuccess) {
            [self pushApplyUrlWeb];
        }
    }];
}

- (void)pushApplyUrlWeb {
    NSLog(@"%@",self.applyUrl);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        XWXAllXieYiController *webVC = [[XWXAllXieYiController alloc] init];
        webVC.tileStr = @"信用卡申请";
        webVC.urlStr = self.applyUrl;
        webVC.isPushFrom = YES;
        webVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC animated:YES];
    });
}

@end
